package cc.lj.internal.coupons.infrastructure.repository.mysql;

import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.model.BasePageHelper;
import cc.lj.internal.core.model.PageParam;
import cc.lj.internal.coupons.domain.ticket.repository.ITicketMainRepository;
import cc.lj.internal.coupons.infrastructure.dao.ITicketMainMapper;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import cc.lj.internal.coupons.infrastructure.po.TicketMainPO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 电子券主表　mysql仓储实现
 * @author liangxifeng
 * @date 2023-06-14
 */
@Repository("ticketMainMysqlRespository")
public class TicketMainMysqlRespository implements ITicketMainRepository,BasePageHelper<TicketMainPO> {
    @Autowired
    private ITicketMainMapper mapper;
    @Override
    public List list(TicketMainPO entityPO) {
        QueryWrapper<TicketMainPO> wrapper = new QueryWrapper<>(entityPO);
        List<TicketMainPO> entitys = mapper.selectList(wrapper);
        if(entitys == null)
        {
            throw new ServiceException(CoupousExceptionEnum.TICKET_MAIN_LIST_NULL);
        }
        return entitys;
    }

    @Override
    public Integer updateById(TicketMainPO entityPO) {
        Integer res = mapper.updateById(entityPO);
        if (res != 1)
        {
            throw new ServiceException(CoupousExceptionEnum.UPDATE_TICKET_MAIN_FAILED);
        }
        return res;
    }

    @Override
    public TicketMainPO getById(Integer id) {
        TicketMainPO entityPO =  mapper.selectById(id);
        if(entityPO==null) {
            throw new ServiceException(CoupousExceptionEnum.TICKET_MAIN_NOT_FOUND);
        }
        return entityPO;
    }

    @Override
    public PageInfo<TicketMainPO> selectPage(PageParam pageParam) {
        return this.page(pageParam);
    }


    @Override
    public List<TicketMainPO> getList(TicketMainPO ticketMainPO) {
        return this.list(ticketMainPO);
    }

    @Override
    public List<TicketMainPO> selectIds(List<Integer> idList) {
        return mapper.selectBatchIds(idList);
    }

    @Override
    public Integer updateByIds(List<Integer> idList, TicketMainPO ticketMainPO) {
        UpdateWrapper<TicketMainPO> updateWrapper = new UpdateWrapper();
        updateWrapper.in("id",idList); //通过主键where in 修改
        Integer rows = mapper.update(ticketMainPO,updateWrapper);
        //返回影响行数
        return rows;
    }
}
